package com.gxc.webcore.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 字符串工具类
 * 
 * @author gxc
 * 
 */
public class StringUtil {

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
	@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
	public final static Map hexMap = new HashMap() {
		{
			put("0", 0);
			put("1", 1);
			put("2", 2);
			put("3", 3);
			put("4", 4);
			put("5", 5);
			put("6", 6);
			put("7", 7);
			put("8", 8);
			put("9", 9);
			put("a", 10);
			put("A", 10);
			put("b", 11);
			put("B", 11);
			put("c", 12);
			put("C", 12);
			put("d", 13);
			put("D", 13);
			put("e", 14);
			put("E", 14);
			put("f", 15);
			put("F", 15);
		}
	};

	/**
	 * 获取uuid
	 * 
	 * @return
	 */
	public static String getUUID() {
		String temp = UUID.randomUUID().toString().toLowerCase();
		String uuid = temp.substring(0, 8) + temp.substring(9, 13)
				+ temp.substring(14, 18) + temp.substring(19, 23)
				+ temp.substring(24, 36);
		return uuid;
	}

	/**
	 * 将十六进制字符串转换成byte[]
	 * 
	 * @param hexString
	 * @return
	 */
	public static byte[] hexString2byteArray(String hexString) {
		if (hexString.length() / 2 == 1) {
			hexString = "0" + hexString;
		}
		byte[] bytes = new byte[hexString.length() / 2];
		int d1, d2;
		for (int i = 0; i < hexString.length(); i++, i++) {
			d1 = (Integer) hexMap.get(hexString.substring(i, i + 1));
			d2 = (Integer) hexMap.get(hexString.substring(i + 1, i + 2));
			bytes[i / 2] = (byte) (d1 * 16 + d2);
		}
		return bytes;
	}

	/**
	 * 转换字节数组为16进制字符串
	 * 
	 * @param b
	 *            字节数组
	 * @return 16进制字符串
	 */
	public static String byteArray2hexString(byte[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			sb.append(byte2hexString(b[i]));
		}

		return sb.toString();
	}

	/**
	 * 将普通字符串转换成十六进制字符串
	 * 
	 * @param s
	 * @return
	 */
	public static String stringToHexString(String s) {
		return StringUtil.byteArray2hexString(s.getBytes());
	}

	/**
	 * 将byte[]转换成普通字符串
	 * 
	 * @param bytes
	 * @return
	 */
	public static String byteArrayToString(byte[] bytes) {
		try {
			return new String(bytes, 0, bytes.length, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将十六进制字符串转换成普通字符串
	 * 
	 * @param hexString
	 * @return
	 */
	public static String hexStringToString(String hexString) {
		return StringUtil.byteArrayToString(StringUtil
				.hexString2byteArray(hexString));
	}

	/**
	 * 转换字节数为16进制字符串
	 * 
	 * @param b
	 *            byte数值
	 * @return 16进制字符串
	 */
	public static String byte2hexString(byte b) {
		int d1 = (b >>> 4) & 0xF;
		int d2 = b & 0xF;

		return hexDigits[d1] + hexDigits[d2];
	}

	/**
	 * 获得0-9的随机数字符串
	 * 
	 * @param length
	 *            返回字符串的长度
	 * @return String
	 */
	public static String getRandomNumber(int length) {
		Random random = new Random();
		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < length; i++) {
			buffer.append(random.nextInt(10));
		}
		return buffer.toString();
	}

	/**
	 * 获得0-9,a-z,A-Z范围的随机字符串
	 * 
	 * @param length
	 *            字符串长度
	 * @return String
	 */
	public static String getRandomChar(int length) {
		char[] chr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
				'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
				'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
				'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
				'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
				'X', 'Y', 'Z' };

		Random random = new Random();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			buffer.append(chr[random.nextInt(62)]);
		}

		return buffer.toString();
	}

	/**
	 * 判断字符串数组中是否包含某字符串
	 * 
	 * @param substring
	 *            某字符串
	 * @param source
	 *            源字符串数组
	 * @return 包含则返回true，否则返回false
	 */
	public static boolean isContains(String substring, String[] source) {
		if (source == null || source.length == 0) {
			return false;
		}

		for (int i = 0; i < source.length; i++) {
			String aSource = source[i];
			if (aSource.equals(substring)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 判断字符是否为空
	 * 
	 * @param str
	 *            某字符串
	 * @return 为null或为空串则返回true，否则返回false
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	/**
	 * 判断字符是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isTrimEmpty(String str) {
		return str == null || str.trim().length() == 0;
	}

	/**
	 * 首字母大写
	 * 
	 * @param str
	 *            字符串
	 * @return 首字符大写后的字符串
	 */
	public static String upFirstChar(String str) {
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}

	/**
	 * 首字母小写
	 * 
	 * @param str
	 *            字符串
	 * @return 首字符大写后的字符串
	 */
	public static String lowerFirstChar(String str) {
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}

}
